package com.example.wechar.contorller;

import com.example.wechar.context.ContextMessageFactory;
import com.example.wechar.pojo.AccessTokenBean;
import com.example.wechar.pojo.TextMessage;
import com.example.wechar.service.UserInfoService;
import com.example.wechar.utils.HttpClientUtil;
import com.example.wechar.utils.MessageType;
import com.example.wechar.utils.WxCharConstant;
import com.example.wechar.utils.XmlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpUtils;
import java.util.Map;

@RestController
public class UserInfoController {

    @Value("${wx.appId}")
    private String addId;
    @Value("${wx.secret}")
    private String secret;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private ContextMessageFactory contextMessageFactory;

    @RequestMapping("/accessToken")
    public String getAccessToken() {
        AccessTokenBean accessTokenBean = userInfoService.getAccesstoken();
        return accessTokenBean.getAccessToken();
    }

    @RequestMapping("/createMenu")
    public void createView() {
//        String content = WxCharConstant.CREATE_MENU_URL.replace("APPID",appId)
//                .replace("REDIRECT_URI",callBack+"/wx/getCode")
//                .replace("SCOPE","snsapi_userinfo");
        String json = "{\n" +
                "     \"button\":[\n" +
                "     {\t\n" +
                "          \"type\":\"click\",\n" +
                "          \"name\":\"今日歌曲\",\n" +
                "          \"key\":\"V1001_TODAY_MUSIC\"\n" +
                "      },\n" +
                "      {\n" +
                "           \"name\":\"菜单\",\n" +
                "           \"sub_button\":[\n" +
                "           {\t\n" +
                "               \"type\":\"view\",\n" +
                "               \"name\":\"搜索\",\n" +
                "               \"url\":\"http://www.soso.com/\"\n" +
                "            },\n" +
                "            {\n" +
                "               \"type\":\"click\",\n" +
                "               \"name\":\"赞一下我们\",\n" +
                "               \"key\":\"V1001_GOOD\"\n" +
                "            }]\n" +
                "       }]\n" +
                " }";
        String accessToken = getAccessToken();
        System.out.println(accessToken);
        try {
            String s = HttpClientUtil.doPostJson(WxCharConstant.CREATE_MENU_URL.replace("ACCESS_TOKEN", accessToken), json);
            System.out.println(s);
            //String s = HttpUtils.sendPost(WxCharConstant.CREATE_MENU_URL.replace("ACCESS_TOKEN",accessToken), json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 信息进行回复
     * @param message
     * @return
     * @throws Exception
     */
    @PostMapping("/wx")
    public String sendPost(@RequestBody String message) throws Exception {
        System.out.println("接收到微信公众平台消息: "+message);
        Map<String,Object> stringObjectMap = XmlUtils.xmlStrToMap(message);
        MessageType msgType = MessageType.getType(stringObjectMap.get("MsgType").toString());
        return contextMessageFactory.doAction(msgType,stringObjectMap);
    }
}
